<template>
  <div class="apply">
    <div class="tou">
      <el-button type="primary" @click="handleput('')">
        添加
        <i class="el-icon-plus el-icon--right"></i>
      </el-button>
    </div>
    <div class="table">
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark">
        <el-table-column prop="name" label="分组名称"></el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleDel(scope.row)"
              >删除</el-button
            >
            <el-button type="primary" size="mini" @click="handleput(scope.row)"
              >编辑</el-button
            >
            <el-button type="primary" size="mini" @click="fenpei(scope.row)"
              >分配奖项</el-button
            >
            <el-button type="primary" size="mini" @click="dels(scope.row)"
              >奖项列表</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :visible.sync="Visible" width="30%" center>
      <el-form
        :model="ruleForm"
        :rules="rules1"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分组名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <div class="fenye">
      <!-- <el-pagination
        background
        layout="prev, pager, next"
        :total="totalPage"
        :page-size="pageSize"
       
      ></el-pagination> -->
      <el-pagination
        
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :total="totalPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return fmt;
};
import { fz_list, fz_create, fz_del } from "../axios/api";
export default {
  data() {
    return {
      group_id: "",
      arr: true,
      arr1: [],
      menu_id: [],
      id: "",

      defaultProps: {
        children: "children",
        label: "label",
      },
      Visible: false,
      page: 0,
      totalPage: 0,
      currentPage: 1,
      pageSize: 0,
      formLabelWidth: "80px",
      ruleForm: {
        name: "",
        token: sessionStorage.getItem("Token1"),
      },
      dialogFormVisible: false,
      disabled: true,
      tableData: [],
      multipleSelection: [],
      rules1: {
        name: [{ required: true, message: "请输入角色描述", trigger: "blur" }],
      },
    };
  },

  components: {},
  created() {
    this.fz_list();
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.id) {
            this.ruleForm.id = this.id;
          }
          let params = this.ruleForm;
          fz_create(params).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                message: "成功",
                type: "success",
              });
              this.ruleForm.name = "";
              this.ruleForm.id = "";
              this.id = "";
              this.Visible = false;
              this.fz_list();
            } else {
              this.$message({
                message: res.data.msg,
              });
              this.fz_list();
              this.ruleForm.id = "";
              this.id = "";
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.Visible = false;
    },
    handleput(e) {
      console.log(e);
      if (e) {
        this.ruleForm.name = e.name;
        this.id = e.id + "";
      } else {
        this.ruleForm.name = "";
      }

      this.Visible = true;
    },

    fz_list() {
      let params = {
        token: sessionStorage.getItem("Token1"),
        page: this.pageSize,
      };
      fz_list(params).then((res) => {
        console.log(res);

        // this.currentPage = res.data.data.page + 0;
        this.totalPage = res.data.data.totalPage;
        console.log(this.totalPage);
        // this.pageSize = parseInt(res.data.data.pageSize);
        this.tableData = res.data.data.data;
        this.tableData.forEach((item) => {
          var myDate = new Date(item.create_time * 1000);
          myDate.setDate(myDate.getDate());

          item.create_time = myDate.format("yyyy年MM月dd日");
        });
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageSize = val;
      this.fz_list();
    },

    handleDel(e) {
      let params = {
        token: sessionStorage.getItem("Token1"),
        id: e.id,
      };
      fz_del(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.fz_list();
        }
      });
    },
    fenpei(e) {
      console.log(e);
      localStorage.setItem("idse", e.id);
      this.$router.push({ path: "/fenpeis", query: { id: e.id } });
    },
    dels(e) {
      console.log(e);
      localStorage.setItem("idse", e.id);
      this.$router.push({ path: "/delse", query: { id: e.id } });
    },
  },
};
</script>

<style scoped>
.apply {
  width: 100%;
  height: 100%;
  background-color: #fff;
  overflow: hidden;
}
.table {
  height: 80%;
  overflow-y: auto;
  box-sizing: border-box;
  margin: 0 20px;
}
.caozuo {
  box-sizing: border-box;
  padding: 20px;
  display: flex;
  justify-content: space-between;
}
.fenye {
  width: 100%;
  display: flex;
  justify-content: center;
}
.tou {
  margin: 10px 20px;
}
</style>